'use strict';
/**
 * fxl
 */
app.controller('InvestigationCtrl', function($scope, $state,$ionicScrollDelegate,Xalert,$timeout,dialog,$window) {

    $scope.myLoading = false;
    $scope.userid = mqiApp.getUserId();
	function onZHstr(e){
		var zhz = e;
	  	var mile = parseFloat(zhz);
	  	var s = mile.toFixed(3).toString();
	  	var a =  s.split(".");
	  	if(null == a || a.length == 0){
	   		return "";
	  	}else if(a.length == 1){
	   		var zs = a[0];
	   		var w = rs;
	   		return zs;
	  	}else if(a.length == 2){
	   		var zs1 = a[0];
	   		var xs = a[1];
	   		var rs = zs1+"+" +xs;
	   		return rs;
	   	}
	}

	//定位start
//    $scope.ding = function(){
//        Xalert.loading("无网作业中，无法使用定位功能");
//    }
    //定位end
   
	$scope.allEvent = false;//所有的点击事件可触发
	$scope.course = 1;
	$scope.ciType = 'SCI';
	$scope.currentLine = [];
	$scope.tabType = function(val){
		if($scope.allEvent) return;
		if($scope.course == val){
			return false;
		}
		$scope.course = val;
		$ionicScrollDelegate.scrollTop();
//		$scope.currentIndex = 0;
		line();
		initList();
		turNumber();
//		console.log(val)
//		console.log($scope.route.pavement_type)
		if(val ==1){
			$scope.ciType = 'SCI';
			$scope.currentLine = $scope.sciLine;
		}else if(val == 2 && $scope.route.pavement_type == 'LQ'){
			$scope.ciType = 'LQ';
			$scope.currentLine = $scope.lqLine;
		}else if(val == 2 && $scope.route.pavement_type != 'LQ'){
			$scope.ciType = 'SN';
			$scope.currentLine = $scope.snLine;
		}else if(val == 3){
			$scope.ciType = 'BCI';
			$scope.currentLine = $scope.bciLine;
		}else if(val == 4){
			$scope.ciType = 'TCI';
			$scope.currentLine = $scope.tciLine;
		}
//        alert($scope.ciType)
        $scope.list100 = JSON.parse(mqiApp.getRoadConditionsById($scope.userid,$scope.route.road_id, $scope.positions.unit_id, $scope.directions.id,$scope.ciType)).list100;
        zhuan();

	}

	$scope.backGo = function(){
//	    alert($scope.allEvent)
		if($scope.allEvent) return;
		mqiApp.goBack();
	}


	$scope.directions = {//方向
		text: '上行',
		id:'1'
	};

    $scope.road_list = JSON.parse(mqiApp.getAllRoadList($scope.userid,false)).road_list;
//	console.log(angular.toJson($scope.road_list));
	$scope.route = {//路线（默认第一个）
		road_code: $scope.road_list[0].road_code,
		road_name:$scope.road_list[0].road_name,
		road_id:$scope.road_list[0].road_id,
		pavement_type:$scope.road_list[0].unit_list[0].pavement_type,
		myI:'0'
	}

	$scope.positions = {//位置（默认第一个）
      start_stake: $scope.road_list[0].unit_list[0].start_stake,
      unit_id:$scope.road_list[0].unit_list[0].unit_id.split(',')[0],
      end_stake:$scope.road_list[0].unit_list[0].end_stake,
      column:$scope.road_list[0].unit_list[0].column,
      startVal:onZHstr($scope.road_list[0].unit_list[0].start_stake),
      myI:'0',
      allId:$scope.road_list[0].unit_list[0].unit_id
     }

    var directionsObj = '';
    function judgeDirections(luxian,qujian){
        if($scope.road_list[luxian].unit_list[qujian].line_dir == '1'){
            directionsObj = [
                {text: '上行',id:'1'}
            ];
        }else if($scope.road_list[luxian].unit_list[qujian].line_dir == '2'){
            directionsObj = [
                {text: '下行',id:'2'}
            ];
        }else if($scope.road_list[luxian].unit_list[qujian].line_dir == '3'){
            directionsObj = [
                {text: '全幅',id:'3'}
            ];
        }else if($scope.road_list[luxian].unit_list[qujian].line_dir == '1,2'){
            directionsObj = [
                {text: '上行',id:'1'},
                {text: '下行',id:'2'}
            ];
        }else if($scope.road_list[luxian].unit_list[qujian].line_dir == '1,2,3'){
            directionsObj = [
                {text: '上行',id:'1'},
                {text: '下行',id:'2'},
                {text: '全幅',id:'3'}
            ];
        }else if($scope.road_list[luxian].unit_list[qujian].line_dir == '1,3'){
            directionsObj = [
                {text: '上行',id:'1'},
                {text: '全幅',id:'3'}
            ];
        }else if($scope.road_list[luxian].unit_list[qujian].line_dir == '2,3'){
            directionsObj = [
                {text: '下行',id:'2'},
                {text: '全幅',id:'3'}
            ];
        }
        $scope.directions = {//方向
            text: directionsObj[0].text,
            id: directionsObj[0].id
        };
    }
    judgeDirections(0,0);

	$scope.sciLine = [
		{text:'路肩损坏-0'},
		{text:'路肩损坏-2'},
		{text:'边坡坍塌-0'},
		{text:'边坡坍塌-1'},
		{text:'边坡坍塌-2'},
		{text:'水毁冲沟-0'},
		{text:'水毁冲沟-1'},
		{text:'水毁冲沟-2'},
		{text:'路基构造物损坏-0'},
		{text:'路基构造物损坏-1'},
		{text:'路基构造物损坏-2'},
		{text:'路缘石缺损-'},
		{text:'路基沉降-0'},
		{text:'路基沉降-1'},
		{text:'路基沉降-2'},
		{text:'排水不畅-0'},
		{text:'排水不畅-1'},
		{text:'排水不畅-2'}
	];
	$scope.lqLine = [
      {text:'龟裂-0'},
      {text:'龟裂-1'},
      {text:'龟裂-2'},
      {text:'块状裂缝-0'},
      {text:'块状裂缝-2'},
      {text:'纵向裂缝-0'},
      {text:'纵向裂缝-2'},
      {text:'横向裂缝-0'},
      {text:'横向裂缝-2'},
      {text:'沉陷-0'},
      {text:'沉陷-0'},
      {text:'车辙-0'},
      {text:'车辙-2'},
      {text:'波浪拥包-0'},
      {text:'波浪拥包-2'},
      {text:'坑槽-0'},
      {text:'坑槽-2'},
      {text:'松散-0'},
      {text:'松散-2'},
      {text:'泛油-'},
      {text:'块状修补-'},
      {text:'条状修补-'}
     ];
	$scope.snLine = [
		{text:'破碎板-0'},
		{text:'破碎板-2'},
		{text:'裂缝-0'},
		{text:'裂缝-1'},
		{text:'裂缝-2'},
		{text:'板角断裂-0'},
		{text:'板角断裂-1'},
		{text:'板角断裂-2'},
		{text:'错台-0'},
		{text:'错台-2'},
		{text:'拱起-'},
		{text:'边角剥落-0'},
		{text:'边角剥落-1'},
		{text:'边角剥落-2'},
		{text:'接缝料损坏-0'},
		{text:'接缝料损坏-2'},
		{text:'坑洞-'},
		{text:'唧泥-'},
		{text:'露骨-'},
		{text:'块状修补-'},
		{text:'条状修补-'}
	];
	$scope.bciLine = [
		{text:'BRDG_TC_GRADE-1'},
		{text:'BRDG_TC_GRADE-2'},
		{text:'BRDG_TC_GRADE-3'},
		{text:'BRDG_TC_GRADE-4'},
		{text:'BRDG_TC_GRADE-5'},
		{text:'TUNNEL_TC_GRADE-1'},
		{text:'TUNNEL_TC_GRADE-2'},
		{text:'TUNNEL_TC_GRADE-3'},
		{text:'TUNNEL_TC_GRADE-4'},
		{text:'TUNNEL_TC_GRADE-5'},
		{text:'CUL_GRADE-0'},
		{text:'CUL_GRADE-1'},
		{text:'CUL_GRADE-2'},
		{text:'CUL_GRADE-3'},
		{text:'CUL_GRADE-4'}
	];
	$scope.tciLine = [
      {text:'防护设施缺损-0'},
      {text:'防护设施缺损-2'},
      {text:'隔离栅损坏-'},
      {text:'标志缺损-'},
      {text:'标线缺损-'},
      {text:'绿化管护不善-'}
     ];
	$scope.currentLine = $scope.sciLine;
	//console.log($scope.currentLine)
	$scope.lineNumber = '';
	$scope.currentIndex = 0;
	function line(){
		if($scope.course == 1){
			$scope.lineNumber = 18;
		}else if($scope.course == 2 && $scope.route.pavement_type == 'LQ'){
			$scope.lineNumber = 22;
		}else if($scope.course == 2 && $scope.route.pavement_type != 'LQ'){
			$scope.lineNumber = 21;
		}else if($scope.course == 3){
			$scope.lineNumber = 15;
		}else if($scope.course == 4){
			$scope.lineNumber = 6;
		}else{
			$scope.lineNumber = 19;
		}
	//“0”数据      start
		$scope.zeroList = [];//“0”数据
		$scope.zeroList.length = Number($scope.positions.column);

		for(var i=0;i<$scope.zeroList.length;i++){
			$scope.zeroList[i] = [];
			for(var z=0;z<$scope.lineNumber;z++){
				var item = {
					text:'0'
				};
				$scope.zeroList[i].push(item);
			}
		}
		//console.log($scope.zeroList)
	//“0”数据      end
	}
	line();
	function initList(){
        $scope.nowList = $scope.zeroList[$scope.currentIndex];
    //“--”数据      start
        $scope.emptyList = [];//“--”数据
        for(var i=0;i<$scope.zeroList[0].length;i++){
            var item = {
                text:'--'
            };
            $scope.emptyList.push(item);
        }
    //“--”数据      end
        //$scope.clickFront = true;//前100按钮，默认开始不可点击
        $scope.clickAfter = false;//后100按钮
        //$scope.frontList = $scope.emptyList;//前100数据为空
        if($scope.currentIndex == 0){
           $scope.frontList = $scope.emptyList;//前100数据为空
           $scope.clickFront = true;//前100按钮，默认开始不可点击
        }else{
           $scope.frontList = $scope.zeroList[$scope.currentIndex - 1];
           $scope.clickFront = false;
        }
        if($scope.zeroList.length == 1){
            $scope.clickFront = true;//前100按钮
            $scope.clickAfter = true;//后100按钮
            $scope.afterList = $scope.emptyList;//后100数据为空
            $scope.allClick = true;//前后都不可点
        }else{
            if($scope.currentIndex == (Number($scope.zeroList.length) - 1)){
                $scope.afterList = $scope.emptyList;
            }else{
                $scope.afterList = $scope.zeroList[$scope.currentIndex + 1];
                $scope.allClick = false;
            }

        }

    }
	initList();


	//离线调取
    $scope.list100 = JSON.parse(mqiApp.getRoadConditionsById($scope.userid,$scope.route.road_id, $scope.positions.unit_id, $scope.directions.id,$scope.ciType)).list100;
//console.log(angular.toJson($scope.list100))
	//安卓模拟数据
	//$scope.list100 = {"list100":[{"value":"66","x":100,"y":3},{"value":"97","x":100,"y":0},{"value":"39","x":100,"y":6},{"value":"79","x":200,"y":13}]}.list100;
	//console.log($scope.list100)

	function zhuan(){
		var b = '';
		for(var q=0;q<$scope.list100.length;q++){
			var myX = ($scope.list100[q].x)/100 - 1;
			var myY = $scope.list100[q].y;
			$scope.zeroList[myX][myY].text = $scope.list100[q].value;
		}

	}
	zhuan();

	function turNumber(){
		var str = $scope.positions.startVal.split("+");
		if($scope.currentIndex == 0){
			$scope.thisVal = str[0] + '+000';
		}else{
			$scope.thisVal = str[0] + '+'+ ($scope.currentIndex*100);
		}
		//console.log($scope.thisVal);
	}
	turNumber();

	//点击,前100
	$scope.onFront = function(){
        if($scope.allEvent) return;
        if($scope.allClick) return;//前后都不点
        $scope.clickAfter = false;//后100可点击
        //if($scope.clickFront) return;
        if($scope.currentIndex == 0){
            $scope.frontList = $scope.emptyList;
            $scope.clickFront = true;//前100不可点
            //判断是否是第一个区间
            if($scope.positions.myI == '0'){
                return false;
            }else{
                $scope.clickFront = false;
                //跳转上一条区间
                $scope.jumpFront = true;
            }
            if($scope.jumpFront == true){
                $scope.jumpFront = false;
                var luxianVal = Number($scope.route.myI);
                var qujianVal = Number($scope.positions.myI) - 1;

                $scope.positions = {//位置
                     start_stake: $scope.road_list[luxianVal].unit_list[qujianVal].start_stake,
                     unit_id:$scope.road_list[luxianVal].unit_list[qujianVal].unit_id.split(',')[0],
                     end_stake:$scope.road_list[luxianVal].unit_list[qujianVal].end_stake,
                     column:$scope.road_list[luxianVal].unit_list[qujianVal].column,
                     startVal:onZHstr($scope.road_list[luxianVal].unit_list[qujianVal].start_stake),
                     myI:qujianVal,
                     allId:$scope.road_list[luxianVal].unit_list[qujianVal].unit_id
                }
                judgeDirections(luxianVal,qujianVal);//改变方向，取第一个
                $scope.currentIndex = Number($scope.positions.column) - 1;

                line();initList();
                $scope.list100 = JSON.parse(mqiApp.getRoadConditionsById($scope.userid,$scope.route.road_id, $scope.positions.unit_id, $scope.directions.id,$scope.ciType)).list100;


            }
        }else{
            $scope.currentIndex = Number($scope.currentIndex) -1;
            if($scope.currentIndex == 0){
                $scope.frontList = $scope.emptyList;
                $scope.clickFront = true;//前100不可点
            }else{
                $scope.frontList = $scope.zeroList[$scope.currentIndex -1];
                $scope.clickFront = false;
            }

            $scope.nowList = $scope.zeroList[$scope.currentIndex];
            $scope.afterList = $scope.zeroList[$scope.currentIndex +1];
        }
        turNumber();
    }

	//点击,后100
	$scope.onAfter = function(){
        if($scope.allEvent) return;
        if($scope.allClick) return;
        $scope.clickFront = false;//前100可点击
        if($scope.clickAfter) return;
        $scope.currentIndex = Number($scope.currentIndex) +1;
        $scope.frontList = $scope.zeroList[$scope.currentIndex -1];//前
        $scope.nowList = $scope.zeroList[$scope.currentIndex];//当

        if($scope.currentIndex == ($scope.zeroList.length -1)){
            $scope.afterList = $scope.emptyList;
            //$scope.clickAfter = true;
            //判断是否是最后一个区间
            if($scope.positions.myI == ($scope.road_list[$scope.route.myI].unit_list.length - 1)){
                //禁止点击
                $scope.clickAfter = true;
                $scope.jumpAfter = false;
            }
        }else if($scope.currentIndex == $scope.zeroList.length){
            //切换到下一个区间
            $scope.jumpAfter = true;
        }else{
            $scope.afterList = $scope.zeroList[$scope.currentIndex +1];
            $scope.clickAfter = false;
        }

        if($scope.jumpAfter == true){//跳转下一条区间
            $scope.jumpAfter = false;
            var luxianVal = Number($scope.route.myI);
            var qujianVal = Number($scope.positions.myI) + 1;
            $scope.positions = {//位置
                start_stake: $scope.road_list[luxianVal].unit_list[qujianVal].start_stake,
                unit_id:$scope.road_list[luxianVal].unit_list[qujianVal].unit_id.split(',')[0],
                end_stake:$scope.road_list[luxianVal].unit_list[qujianVal].end_stake,
                column:$scope.road_list[luxianVal].unit_list[qujianVal].column,
                startVal:onZHstr($scope.road_list[luxianVal].unit_list[qujianVal].start_stake),
                myI:qujianVal,
                allId:$scope.road_list[luxianVal].unit_list[qujianVal].unit_id
            }
            judgeDirections(luxianVal,qujianVal);//改变方向，取第一个
            $scope.currentIndex = 0;
            line();initList();
            $scope.list100 = JSON.parse(mqiApp.getRoadConditionsById($scope.userid,$scope.route.road_id, $scope.positions.unit_id, $scope.directions.id,$scope.ciType)).list100;

        }
        turNumber();
    }


	var choice = [];
	$scope.selectVal = '';
	$scope.selectList = false;
	$scope.closeDiv = function(){
		$scope.selectList = false;
	}
	$scope.select = function(val){
		if($scope.allEvent) return;
		if(val == 1){//选择路线
	       $scope.routeList = [];
	       for(var i=0;i<$scope.road_list.length;i++){
	       		var item = {
	       			text: $scope.road_list[i].road_code+$scope.road_list[i].road_name,
                               road_code:$scope.road_list[i].road_code,
	       			name:$scope.road_list[i].road_name,
	       			id:$scope.road_list[i].road_id,
	       			pavement_type:$scope.road_list[i].unit_list[0].pavement_type,
	       			myI:i
	       		}
	       		$scope.routeList.push(item);
	       	}
	        choice = $scope.routeList;
	       $scope.selectVal = '1';
		}else if(val == 2){
			$scope.positionList = [];
	        for(var z=0;z<$scope.road_list[$scope.route.myI].unit_list.length;z++){
    			var items = {
                     text : onZHstr($scope.road_list[$scope.route.myI].unit_list[z].start_stake),
                     unit_id:$scope.road_list[$scope.route.myI].unit_list[z].unit_id.split(',')[0],
                     end_stake:$scope.road_list[$scope.route.myI].unit_list[z].end_stake,
                     column:$scope.road_list[$scope.route.myI].unit_list[z].column,
                     start_stake:$scope.road_list[$scope.route.myI].unit_list[z].start_stake,
                     line_dir:$scope.road_list[$scope.route.myI].unit_list[z].line_dir,
                     allId:$scope.road_list[$scope.route.myI].unit_list[z].unit_id,
					 pavement_type:$scope.road_list[$scope.route.myI].unit_list[z].pavement_type,
                }
    			$scope.positionList.push(items);
    		}
    		//console.log($scope.positionList)
    		choice = $scope.positionList;
	        $scope.selectVal = '2';
		}else if(val == 3){
			judgeDirections($scope.route.myI,$scope.positions.myI);
            choice = directionsObj;
	        $scope.selectVal = '3';
		}
	    $scope.selectList = true;
	    $scope.choiceList = choice;

	}

	$scope.buttonClicked = function(item,$index){
//		console.log($scope.selectVal)
		if($scope.selectVal == 1){
  			if(item.id != $scope.route.road_id){
        		$scope.route.road_code = item.road_code;
        		$scope.route.road_name = item.name;
        		$scope.route.road_id = item.id;
        		$scope.route.pavement_type = item.pavement_type;
        		$scope.route.myI = item.myI;
        		if($scope.course == 2){
        		    $scope.ciType = $scope.route.pavement_type;
        		}
        		//lantern();
        		$scope.positions = {//位置
                     start_stake: $scope.road_list[item.myI].unit_list[0].start_stake,
                     unit_id:$scope.road_list[item.myI].unit_list[0].unit_id.split(',')[0],
                     end_stake:$scope.road_list[item.myI].unit_list[0].end_stake,
                     column:$scope.road_list[item.myI].unit_list[0].column,
                     startVal:onZHstr($scope.road_list[item.myI].unit_list[0].start_stake),
                     myI:'0',
                     allId:$scope.road_list[item.myI].unit_list[0].unit_id
                }
//        		console.log(angular.toJson($scope.route))
//        		console.log(angular.toJson(item))
        		$scope.currentIndex = 0;
        		//console.log($scope.positions)
        		line();
        		initList();
        		judgeDirections($scope.route.myI,$scope.positions.myI);//切换路线时对应改变第一个路线方向的id

        		if($scope.course == 2){
                    $scope.ciType = $scope.route.pavement_type;
                }
        		$scope.list100 = JSON.parse(mqiApp.getRoadConditionsById($scope.userid,$scope.route.road_id, $scope.positions.unit_id, $scope.directions.id,$scope.ciType)).list100;
        		zhuan();
        		turNumber();
        	}

  		}else if($scope.selectVal == 2){
  			if(item.text != $scope.positions.start_stake){
  				$scope.route.pavement_type = item.pavement_type;
  				$scope.positions = {//位置
                     start_stake: item.start_stake,
                     unit_id:item.unit_id.split(',')[0],
                     end_stake:item.end_stake,
                     column:item.column,
                     startVal:onZHstr(item.text),
                     myI:$index,
                     allId:item.unit_id
                }
  				
        		$scope.currentIndex = 0;
//        		console.log($scope.positions)
        		line();
        		initList();
        		judgeDirections($scope.route.myI,$scope.positions.myI);//切换区间时对应改变方向的id
        		if($scope.course == 2){
                    $scope.ciType = $scope.route.pavement_type;
                }
        		
        		$scope.list100 = JSON.parse(mqiApp.getRoadConditionsById($scope.userid,$scope.route.road_id, $scope.positions.unit_id, $scope.directions.id,$scope.ciType)).list100;
        		zhuan();
        		turNumber();
        	}
  			
  		}else if($scope.selectVal == 3){
  		    $scope.positions.unit_id = $scope.positions.allId.split(',')[$index];
  			if(item.id != $scope.directions.id){
        		$scope.directions.text = item.text;
        		$scope.directions.id = item.id;
        		$scope.currentIndex = 0;
        		line();
        		initList();
        		
        		if($scope.course == 2){
                    $scope.ciType = $scope.route.pavement_type;
                }
        		$scope.list100 = JSON.parse(mqiApp.getRoadConditionsById($scope.userid,$scope.route.road_id, $scope.positions.unit_id, $scope.directions.id,$scope.ciType)).list100;
        		zhuan();
        		turNumber();
        	}
  		}
  	//	console.log(angular.toJson($scope.list100))
	}
	
	//计算器
	/*$scope.calculator = false;
	$scope.numberlist = [
		{text:'1',value:'1'},
		{text:'2',value:'2'},
		{text:'3',value:'3'},
		{text:'4',value:'4'},
		{text:'5',value:'5'},
		{text:'6',value:'6'},
		{text:'7',value:'7'},
		{text:'8',value:'8'},
		{text:'9',value:'9'},
		{text:'0',value:'0'},
	];
	$scope.init = function(){//初始化
		$scope.textView = "";
		$scope.fuhao = "+";
	    $scope.firstNumber = "";
	    $scope.secondNumber = "";
	    $scope.all = false;//所有的数字可点击
		//$scope.operation = false;//运算符可点击
		$scope.noBack = true;//不回退
		$scope.ok = false;//可计算
		$scope.allEvent = false;
	}
	$scope.init();
	
	$scope.highlight = null;
	$scope.indexVal = '';
	$scope.initNumber = '';
	$scope.reckon = function(item,$index){
		if($scope.allEvent) return;
		$scope.highlight = $index;
		$scope.indexVal = $index;
		$scope.firstNumber = item.text;
		$scope.initNumber = item.text;
		//$scope.textView = item.text;
		$scope.textView = $scope.firstNumber + $scope.fuhao;
		
		$scope.calculator = true;
		
		$scope.allEvent = true;
		$scope.nowList[$scope.indexVal].text = $scope.textView;
	}
	$scope.closeReckon = function(){
		$scope.calculator = false;
		$scope.allEvent = false;
		$scope.highlight = null;
		$scope.init();
		$scope.nowList[$scope.indexVal].text = $scope.initNumber;
	}
	
	function judgeNumber(){
		if($scope.secondNumber.indexOf(".") != -1 ){
			//小数点后保留1位
			var spotLength = $scope.secondNumber.toString().split('.')[1].length;
			if(spotLength == 2){
				$scope.all = true;
			}
		}else{
			if($scope.secondNumber.length >= 2){//整数部分是2位
				$scope.all = true;
			}
		}
	}
	$scope.amount = function(item){//数字
		if($scope.all) return;
		$scope.noBack = false;
		$scope.secondNumber = $scope.secondNumber + item.text;
		$scope.textView = $scope.firstNumber + $scope.fuhao + $scope.secondNumber;
		judgeNumber();
		$scope.nowList[$scope.indexVal].text = $scope.textView;
	}
	$scope.addOrReduce = function(val){
		if($scope.fuhao == '-'){
			$scope.fuhao = '+';
		}else{
			$scope.fuhao = '-';
		}
		$scope.textView = $scope.firstNumber + $scope.fuhao + $scope.secondNumber;
		$scope.nowList[$scope.indexVal].text = $scope.textView;
	}*/
	/*$scope.backspace = function(){//退格
		if($scope.noBack) return;//禁止回退
		if($scope.secondNumber == ''){
			$scope.noBack = true;
		}else{
			$scope.noBack = false;
			$scope.secondNumber = $scope.secondNumber.substr(0,$scope.secondNumber.length-1);
			$scope.textView = $scope.firstNumber + $scope.fuhao + $scope.secondNumber;
		}
		$scope.all = false;
		judgeNumber();
		$scope.nowList[$scope.indexVal].text = $scope.textView;
	}*/
	/*$scope.dian = function(){//小数点
		if($scope.secondNumber == ''){//有整数时才可输入小数
			return false;
		}else{
			//小数点
			if($scope.textView.indexOf(".") == -1 ){
				$scope.secondNumber = $scope.secondNumber + '.';
				$scope.textView = $scope.firstNumber + $scope.fuhao + $scope.secondNumber;
				$scope.all = false;//点完小数点后，可输入数字
			}
		}
		$scope.nowList[$scope.indexVal].text = $scope.textView;
	}*/
	/*$scope.confirm = function(){//确认
		if($scope.ok) return;//禁止重复点击
		if($scope.secondNumber != ''){
			var spstr = $scope.secondNumber.split('');//判断最后一位是否小数点
			if(spstr[spstr.length-1] == '.'){
				return false;
			};
			if($scope.fuhao == "+"){
				$scope.textView = Number($scope.firstNumber) + Number($scope.secondNumber);
			}else{
				$scope.textView = Number($scope.firstNumber) - Number($scope.secondNumber);
			}
			$scope.ok = true;
			//console.log($scope.textView);
			$scope.nowList[$scope.indexVal].text = $scope.textView;
			//console.log($scope.indexVal)
			$scope.highlight = null;
			$scope.calculator = false;
			//显示转圈
			//调接口
			//成功之后，关闭转圈，初始化计算器
			dialog.openLoadingDialog();
			$scope.keepValue($scope.ciType);
			$timeout(function() {
	            dialog.closeIonIcLoading();
	            $scope.init();
	        }, 2000);
			
			
		}
	}*/


	//保存修改的数据
	$scope.keepValue = function(val,lineArray) {
		//console.log($scope.currentLine)
		var str = $scope.currentLine[$scope.indexVal].text.split("-");
		$scope.dss_type = str[0];
        $scope.dss_degree = str[1];
        //alert($scope.dss_degree);

//		console.log($scope.positions)
//		console.log($scope.textView)
		//return false;
		
		var data = {
			userid:$scope.userid,
			road_id:$scope.route.road_id,
			unit_id:$scope.positions.unit_id,
			x:($scope.currentIndex + 1)*100,
			y:$scope.indexVal,
			value:$scope.textView,
			line_dir:$scope.directions.id,
			start_stake:$scope.positions.start_stake,
			end_stake:$scope.positions.end_stake,
			type:val
		}
		mqiApp.updateRoadCondition(angular.toJson(data));
		
	};
	
	
	//计算器2
	$scope.calculator = false;
	$scope.numberlist = [
		{text:'1',value:'1'},
		{text:'2',value:'2'},
		{text:'3',value:'3'},
		{text:'4',value:'4'},
		{text:'5',value:'5'},
		{text:'6',value:'6'},
		{text:'7',value:'7'},
		{text:'8',value:'8'},
		{text:'9',value:'9'},
		{text:'0',value:'0'},
	];
	$scope.init = function(){//初始化
		$scope.textView = "";
		$scope.fuhao = "+";
	    $scope.firstNumber = "";
	    $scope.secondNumber = "";
	    $scope.all = false;//所有的数字可点击
	    
		$scope.noBack = true;//不回退
		$scope.ok = false;//可计算
		$scope.allEvent = false;
	}
	$scope.init();
	$scope.highlight = null;
	$scope.indexVal = '';
	$scope.initNumber = '';
	
	var yy = 0;
 	var headHandle = $ionicScrollDelegate.$getByHandle("headContainerHandle");
	/*var s = $ionicScrollDelegate.$getByHandle('headContainerHandle').getScrollPosition();
	console.log(s)*/
	//获取点击时的高度
	$scope.startHeight = '';
	function getHeight(){
		var nowTransform = $(".scroll")[0].style.transform;
		if(!nowTransform){
			nowTransform = $(".scroll")[0].style.webkitTransform; 
		}
		var startIndex = nowTransform.indexOf('(')+1;
		var endIndex = nowTransform.indexOf(')');
		var nowTranslate = nowTransform.substring(startIndex,endIndex);
		var translateArr = nowTranslate.split(",");
		var myScrollTop,myScrollLeft;
		myScrollLeft=translateArr[0];
		myScrollTop=translateArr[1];
		
		//console.log(myScrollTop)
		$scope.startHeight = myScrollTop.replace(/(^\s*)|(\s*$)/g, "");
		//console.log($scope.startHeight)
		
	}
	$scope.reckon = function(item,$index){
		$scope.point = true;
		// 不是平方米 禁止输入小数点
		if($scope.course == 1){
			if($index >= 2 && $index <= 10 || $index > 11){
				$scope.point = false;
			}else{
				$scope.point = true;
			}
		}
		if($scope.course == 4){
			if($index < 4){
				$scope.point = false;
			}else{
				$scope.point = true;
			}
		}
		getHeight();
		//点击出现计算器，把所点击的位置置顶
  		yy = $('.rr')[$index].offsetTop;
  		
  		
  		$timeout(function() {
           headHandle.scrollTo(0,yy,300); 
        }, 100);
  		
		
		if($scope.allEvent) return;
		$scope.highlight = $index;//高亮样式
		$scope.indexVal = $index;
		$scope.initNumber = item.text;
		$scope.calculator = true;
		$scope.firstNumber = item.text;
		$scope.allEvent = true;
		$scope.textView = $scope.firstNumber + $scope.fuhao;
		//$scope.nowList[$scope.indexVal].text = $scope.textView;
		
	}
	$scope.closeReckon = function(){
		$scope.calculator = false;
		$scope.allEvent = false;
		$scope.highlight = null;
		$scope.init();
		//$scope.nowList[$scope.indexVal].text = $scope.initNumber;
		
		var goStartHeight = 0;
		if($scope.startHeight != '0px'){
			var str = $scope.startHeight.split("-");
			goStartHeight = (str[1].split("px"))[0];
			//console.log(goStartHeight)
		}
		$timeout(function() {
           headHandle.scrollTo(0,goStartHeight,300); 
        }, 100);
		
	}
	function judgeNumber(){
		if($scope.secondNumber.indexOf(".") != -1 ){
			//小数点后保留2位
			var spotLength = $scope.secondNumber.toString().split('.')[1].length;
			if(spotLength == 2){
				$scope.all = true;
			}
		}else{
			if($scope.secondNumber.length >= 4){//整数部分是2位
				$scope.all = true;
			}
		}
	}
	$scope.amount = function(item){//数字
		if($scope.all) return;
		$scope.noBack = false;
		$scope.secondNumber = $scope.secondNumber + item.text;
		$scope.textView = $scope.firstNumber + $scope.fuhao + $scope.secondNumber;
		judgeNumber();
		//$scope.nowList[$scope.indexVal].text = $scope.textView;
	}
	$scope.addOrReduce = function(val){
		//$scope.noBack = false;
		//$scope.all = false;		
		$scope.fuhao = val;
		$scope.textView = $scope.firstNumber + $scope.fuhao + $scope.secondNumber;
		//$scope.nowList[$scope.indexVal].text = $scope.textView;
	}
	$scope.backspace = function(){//退格
		
		if($scope.noBack) return;//禁止回退
		if($scope.secondNumber == ''){
			$scope.noBack = true;
		}else{
			$scope.noBack = false;
			$scope.secondNumber = $scope.secondNumber.substr(0,$scope.secondNumber.length-1);
			$scope.textView = $scope.firstNumber + $scope.fuhao + $scope.secondNumber;
		}
		$scope.all = false;
		judgeNumber();
		//$scope.nowList[$scope.indexVal].text = $scope.textView;
		
		
	}
	$scope.dian = function(){//小数点
		if($scope.point == false) return false;
		if($scope.course == 3) return false;//bci不可输入小数
		if($scope.secondNumber == ''){//有整数时才可输入小数
			return false;
		}else{
			//小数点
			if($scope.secondNumber.indexOf(".") == -1 ){
				$scope.secondNumber = $scope.secondNumber + '.';
				$scope.textView = $scope.firstNumber + $scope.fuhao + $scope.secondNumber;
				$scope.all = false;//点完小数点后，可输入数字
//				console.log($scope.secondNumber)
			}
		}
		//$scope.nowList[$scope.indexVal].text = $scope.textView;
		
	}
	$scope.confirm = function(){//确认
		if($scope.ok) return;//禁止重复点击
		if($scope.secondNumber != ''){
			var spstr = $scope.secondNumber.split('');//判断最后一位是否小数点
			if(spstr[spstr.length-1] == '.'){
				return false;
			};
			
			if($scope.fuhao == "+"){
				$scope.textView = Number($scope.firstNumber) + Number($scope.secondNumber);
			}else{
			    if(Number($scope.secondNumber) > Number($scope.firstNumber)){
                    Xalert.loading('请输入正确值');
                    return false;
                }
				$scope.textView = Number($scope.firstNumber) - Number($scope.secondNumber);
			}
			$scope.ok = true;
//			console.log($scope.textView)
			$scope.nowList[$scope.indexVal].text = $scope.textView;
			$scope.highlight = null;
			$scope.calculator = false;
			//显示转圈
			//调接口
			//成功之后，关闭转圈，初始化计算器
			//$scope.myLoading = true;
			$scope.keepValue($scope.ciType);
			$scope.init();
			/*$timeout(function() {
	            $scope.myLoading = false;

	        }, 2000);*/
	        var goStartHeight = 0;
            if($scope.startHeight != '0px'){
                var str = $scope.startHeight.split("-");
                goStartHeight = (str[1].split("px"))[0];
                //console.log(goStartHeight)
            }
            $timeout(function() {
               headHandle.scrollTo(0,goStartHeight,300);
            }, 100);
		}
		
	}

});